<?php
global $wpdb;
if(isset($_GET['del']))
{
	$url = get_bloginfo('home')."/wp-admin/admin.php?page=hotel-setting&action=hotel-price-table";
	$del = $_GET['del'];
	db_delete_item_price_by_id($del, "hotel_price");
	wp_redirect($url);
}
if(isset($_POST['price_table_hotel']) && $_POST['price_table_hotel']!=null)
	$_SESSION['current_hotel_price_table'] = $_POST['price_table_hotel'];
if(wp_verify_nonce($_POST['act_insert_hotel_price_table'],'act_insert_hotel_price_table'))
{	
	$_SESSION['current_hotel_price_table'] = $_POST['price_table_hotel'];
	
	if(isset($_POST['insert-hotel-price-table']))
	{
		if($_POST['typeroom'] !="" || $_POST['max'] != "" || $_POST['totalofroom'] != "" )
		{
			$insert_code = $_POST['hotel_code'];
			if(intval($_POST['hotel_code'])==intval($_SESSION['insert_code_hotel']))
				db_insert_hotel_price_table($_POST);
		}
	}
	else if(isset($_POST['update-hotel-price-table']))
	{ 
		$_SESSION['current_hotel_price_table'] = $_POST['price_table_hotel2'];
		db_update_hotel_price_table($_POST);
	}
} 
$code = rand(1111,9999);
$_SESSION['insert_code_hotel'] = $code;

//echo  'dsfasf: '.$_SESSION['current_hotel_price_table'];

?>
<style>
.hienthi
{
	display:block;
}
.an
{
	display:none;
}
</style>
<div id="index_page">
	<form action="" method="post" name="insert-price-table">
		<table>
			<tr class="first-header">
				<td colspan="6">
				<?php
				$hotels = db_query_all_post_in_post_type(-1, 'post', 'date', 'DESC', $wpdb->prefix.'display', 'display');
				query_posts($hotels); 
				?>
				<select onchange="this.form.submit()" style="min-width: 400px;" name="price_table_hotel" id="price_table_hotel">
					<option>Select hotel</option>
					<?php
					while (have_posts())
					{
						the_post();
						$postID = get_the_ID();
						if(isset($_SESSION['current_hotel_price_table']) && intval($_SESSION['current_hotel_price_table'])==$postID)
						{
							?>
							<option selected="selected" value="<?php echo $postID;?>"><?php the_title();?></option>
							<?php
						}
						else 
						{
						?>
							<option value="<?php echo $postID;?>"><?php the_title();?></option>
						<?php	
						} 
					} 
					?>
				</select>
				
				<select style="min-width: 400px;" name="price_table_hotel2" id="price_table_hotel2" class="an">
					<option>Select hotel</option>
					<?php
					query_posts($hotels);
					while (have_posts())
					{
						the_post();
						$postID = get_the_ID();
						if(isset($_SESSION['current_hotel_price_table']) && intval($_SESSION['current_hotel_price_table'])==$postID)
						{
							?>
							<option selected="selected" value="<?php echo $postID;?>"><?php the_title();?></option>
							<?php
						}
						else 
						{
						?>
							<option value="<?php echo $postID;?>"><?php the_title();?></option>
						<?php	
						} 
					} 
					?>
				</select>
				
				</td>
			</tr>
			<tr class="first-header">
				<td> Type Room </td>
				<td> Max </td>
				<td> Total of Rooms </td>
				<td> Room empty </td>
				<td> Rate for night </td>
				<td> Insert </td>
			</tr>
			<tr class="second-item">
				<td> 
					<!-- <input type="text" id="typeroom" value="" name="typeroom"> -->
					<?php
					$typeRoom = db_type_room_list(); 
					?>
					<select id="typeroom" name="typeroom">
					<?php
						foreach ($typeRoom as $value) 
						{
							?>
							<option value="<?php echo $value;?>"><?php echo $value;?></option>
							<?php 
						} 
					?>
					</select>
					<input type="hidden" id="hotelID_hidden" name="hotelID_hidden" value=""> 
				</td>
				<td> <input type="text" id="max" value="" name="max"> </td>
				<td> <input type="text" id="totalofroom" value="" name="totalofroom"> </td>
				<td> <input type="text" id="roomempty" value="" name="roomempty"> </td>
				<td> <input type="text" id="ratefornight" value="" name="ratefornight">
					<input type="hidden" value="<?php echo $code;?>" name="hotel_code">
				 </td>
				<td>					
					<ul>
						<li class="sbInsert">							
							<input type="submit" class="tour_insert_price" value="Insert" name="insert-hotel-price-table">
						</li>
						<li class="sbUpdate">
							<input type="submit" class="tour_update_price_hidden" value="Update" name="update-hotel-price-table">
						
							<input type="submit" class="tour_update_price_hidden" value="return" name="return-hotel-price-table">
							<input type="hidden" class="tour_update_price_hidden" id="update-hotel-price-table2" value="<?php echo $_SESSION['current_hotel_price_table'];?>" name="update-hotel-price-table2">
							
						</li>
					</ul> 
				</td>				
			</tr>			
		</table>
		<?php wp_nonce_field('act_insert_hotel_price_table','act_insert_hotel_price_table');?>
	</form>
	<!-- ************************************************************* -->
	<?php
	//$list_price = db_select_all_data("hotel_price");
	$hotelID = intval($_SESSION['current_hotel_price_table']);
	$list_price = db_select_all_data_by_hotelID("hotel_price",$hotelID)
	//print_r($list_price); 
	?>
	<br/>
	<table>
			
			<tr class="first-header">
				<td> Type Room </td>
				<td> Max </td>
				<td> Total of Room </td>
				<td> Room Empty </td>
				<td> Rate of Night </td>				
				<td> Actions </td>
			</tr> 
			<?php
			if($list_price)
			{
			foreach ($list_price as $value) 
			{
			?>
			<tr class="item-second">
				<td> <?php echo $value->typeroom;?></td>
				<td> <?php echo $value->max;?></td>
				<td> <?php echo $value->totalofroom;?></td>
				<td> <?php echo $value->roomempty;?></td>
				<td> <?php echo $value->ratefornight;?> USD</td>
				<td>
				<?php
				$str = $value->ID.'-'.$value->typeroom.'-'.$value->max.'-'.$value->totalofroom.'-'.$value->roomempty.'-'.$value->ratefornight; 
				?>
					<a class="update_hotel" rel="<?php echo $str;?>" href="#">Update</a> &nbsp;|&nbsp;
					<a href="<?php bloginfo('home');?>/wp-admin/admin.php?page=hotel-setting&action=hotel-price-table&del=<?php echo $value->ID;?>">Delete</a>
				</td>								
			</tr>
			<?php 
			}
			} 
			?>
		</table>	
</div>
